package com.itheima.service.impl;


import com.itheima.common.entity.Result;
import com.itheima.dao.OrderSettingDao;
import com.itheima.dto.OrderSettingDTO;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.vo.OrderSettingVO;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * @author 32338
 * @date 2024-05-04 15:19:09
 * @description 预约设置管理
 */
@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * @description
     ***:根据日期修改可预约人数
     **/
    public void editNumberByOrderDate(OrderSettingDTO orderSettingDTO) {
        OrderSetting orderSetting = new OrderSetting();
        BeanUtils.copyProperties(orderSettingDTO,orderSetting);
        orderSettingDao.deleteByDate(orderSetting);//插入之前先删除当天的信息
        orderSettingDao.insert(orderSetting);//插入修改后的预约人数
    }

    /**
     * @description
     ***:根据年月查询对应的预约设置信息
     *
     * @param month*/
    public List<OrderSettingVO> getOrderSettingByMonth(String month) {
        //此时传进来的还是一个字符串类型的参数
        String monthNumber = month.substring(month.indexOf("-")+1);//我们需要的时月份，+1才得到月份
        List<OrderSettingVO> orderSettingVOList = new ArrayList<>();
        List<OrderSetting> orderSettingList = orderSettingDao.getByMonth(monthNumber);

        orderSettingList.stream().forEach(orderSetting -> {
            OrderSettingVO orderSettingVO = new OrderSettingVO();
            BeanUtils.copyProperties(orderSetting,orderSettingVO);
            Calendar c = Calendar.getInstance();//用于获取当前日期只需要Data就行
            c.setTime(orderSetting.getOrderDate());//得到当前数据的日期;
            orderSettingVO.setOrderDate(c.get(Calendar.DATE));
            orderSettingVOList.add(orderSettingVO);
        });

        return orderSettingVOList;
    }
    /**
     * @description
     ***:模板下载
     **/
    public void download(String filename, HttpServletResponse response) {

        InputStream in =this.getClass().getClassLoader().getResourceAsStream("templates/ordersetting_template.xlsx");
        try {
            XSSFWorkbook excel = new XSSFWorkbook(in);
            String fileName=filename.substring(0,filename.lastIndexOf("."));
            response.setHeader("Content-Disposition","attachment;fileName=" + URLEncoder.encode(fileName,"utf-8") +".xlsx");
            response.flushBuffer();
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);
            outputStream.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * @description
     ***:文件上传
     *
     * @return*/
    public Result upload(MultipartFile excelFile) {
        //不为空？
        if(excelFile!=null){
            try {
                //通过文件地址获取到输入流
                InputStream inputStream=excelFile.getInputStream();

                //设置模板
                XSSFWorkbook excel=new XSSFWorkbook(inputStream);

                //获取页,解析文件把文件中的信息上传到数据库中并且更新数据库
                XSSFSheet sheets=excel.getSheet("预约设置模板");

                int length=sheets.getLastRowNum();//的到文件中的最后一行有值的行号
                //遍历日期，更新实质上是删除日期再添加
                for (int i =1; i <=length; i++) {
                    OrderSetting orderSetting=new OrderSetting();
                    XSSFRow row = sheets.getRow(i);
                    //表格日期形式是xxxx/xx/xx可以通过HSSFDateUtil获取
                    Date date = HSSFDateUtil.getJavaDate(row.getCell(0).getNumericCellValue());
                    orderSetting.setOrderDate(date);
                    int number= (int) row.getCell(1).getNumericCellValue();
                    orderSetting.setNumber(number);

                    orderSettingDao.deleteByDate(orderSetting);
                    orderSettingDao.insert(orderSetting);
                }
                inputStream.close();
                excel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

